package controllers

import (
	//"fmt"
	//"log"
	"ktvserver/ktvutil"
)

const DEFAULT_USER = "admin"
const DEFAULT_PASSWORD = "admin"

const SESSION_KEY_USERNAME = "username"
const SESSION_KEY_LEVEL = "level"
const SESSION_KEY_ISLOGIN = "islogin"

// 登录
type LoginController struct {
	BaseController
}

// 登录界面
func (c *LoginController) Get() {
	c.TplNames = "login"
	c.Data["title"] = "用户登录"

	username := c.GetString("username")
	next := c.GetString("next", "/ktv/online")

	c.Data["username"] = username
	c.Data["next"] = next

}

// 处理登录
func (c *LoginController) Post() {
	c.TplNames = "login"
	c.Data["title"] = "用户登录"

	username := c.GetString("username")
	password := c.GetString("password")
	next := c.GetString("next", "/ktv/online")

	if ktvutil.CheckUserPassword(username, password) {
		//登录成功
		c.SetSession(SESSION_KEY_USERNAME, username)
		c.SetSession(SESSION_KEY_LEVEL, "admin")
		c.SetSession(SESSION_KEY_ISLOGIN, "true")
		c.Redirect(next, 302)
		return
	}

	c.Data["username"] = username
	c.Data["next"] = next

}

// 退出
type LogoutController struct {
	BaseController
}

func (c *LogoutController) Get() {
	c.SetSession(SESSION_KEY_USERNAME, "")
	c.SetSession(SESSION_KEY_LEVEL, "")
	c.SetSession(SESSION_KEY_ISLOGIN, "false")
	c.Redirect("/login", 302)
}
